package com.teaphy.coroutinecrawler.flow.operators.context

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    println("runBlocking context: ${currentCoroutineContext()}, thread name: ${Thread.currentThread().name}")

    flow {
        println("flow ->1 context: ${currentCoroutineContext()}, thread name: ${Thread.currentThread().name}")
        emit(1)
        delay(1000)
        coroutineScope {
//            withContext(CoroutineName("AAA")) {
//                emit(2)
//                println("flow ->2 context: ${currentCoroutineContext()}, thread name: ${Thread.currentThread().name}")
//            }
            emit(3)
            println("flow ->3 context: ${currentCoroutineContext()}, thread name: ${Thread.currentThread().name}")
        }
        println("flow ->4 context: ${currentCoroutineContext()}, thread name: ${Thread.currentThread().name}")
    }.map {
        println("map - context: ${currentCoroutineContext()} , thread name: ${Thread.currentThread().name}")
        it
    }
        .onEach {
            println("onEach - context: ${currentCoroutineContext()} , thread name: ${Thread.currentThread().name}")
        }
        .onStart {
            println("onStart - context: ${currentCoroutineContext()}, thread name: ${Thread.currentThread().name} ")
        }
        .flowOn(CoroutineName("AAA") + Dispatchers.IO)
        .onCompletion {
            println("onCompletion - context: ${currentCoroutineContext()} , thread name: ${Thread.currentThread().name}")
        }
        .collect {
            println("collect - context: ${currentCoroutineContext()} , thread name: ${Thread.currentThread().name}")
        }
}